package com.zixun.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zixun.Models.MessageItem;
import com.zixun.R;


import java.util.List;


public class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.MessageViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private List<MessageItem> datas;
    private Context context;
    private LayoutInflater inflater;
    private View mHeaderView;


    public OnItemClickListener mListener;



    public  interface  OnItemClickListener {
          void onItemClick(int position, String data);
    }
    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }
    public DoctorAdapter(Context context, List<MessageItem> datas) {
        this.context = context;
        this.datas = datas;
        inflater = LayoutInflater.from(context);
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null)
            return TYPE_NORMAL;
        if (position == 0)
            return TYPE_HEADER;

        return TYPE_NORMAL;
    }

    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) {
            return new MessageViewHolder(mHeaderView);
        } else {
            View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.doctoritem_layout, parent, false);
            return new MessageViewHolder(layout);
        }
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public void onBindViewHolder(MessageViewHolder viewHolder, int position) {
        if(getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(viewHolder);
        final String data = datas.get(pos).getContent();
        final int imageUri = datas.get(pos).getImageId();
        if(viewHolder instanceof MessageViewHolder) {
            ((MessageViewHolder) viewHolder).txt_content.setText(data);
            ((MessageViewHolder) viewHolder).imageView.setBackgroundResource(imageUri);
            if(mListener == null) return;
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(pos, data);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? datas.size() : datas.size() + 1;
    }

    public class MessageViewHolder extends RecyclerView.ViewHolder {

        public TextView txt_content;
        public ImageView imageView;
        public MessageViewHolder(View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            txt_content = (TextView) itemView.findViewById(R.id.txt_content);
            imageView = (ImageView) itemView.findViewById(R.id.home_icon);
        }
    }

}
